package com.yisu.auction.manager.website.controller.memberInfo;

import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.Prize;
import com.yisu.auction.ewf.entity.RechargeableCard;
import com.yisu.auction.manager.core.service.IRechargeableCardService;
import com.yisu.auction.manager.core.service.IWinnerService;
import com.yisu.auction.manager.domain.request.memberPackage.WinnerRequest;
import com.yisu.auction.manager.domain.vo.winnerInfoPackageVos.WinnerVo;
import com.yisu.auction.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/25
 */
@Controller
@RequestMapping(value = "/winner")
public class WinnerController extends BaseController{
    @Resource
    private IWinnerService iWinnerService;
    @Resource
    private IRechargeableCardService iRechargeableCardService;

    /**
     * author:黄萧林
     * Business Description:
     * @param model
     * @param winnerRequest
     * @return
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, WinnerRequest winnerRequest){
        try {
            if (winnerRequest.getKeyword() != null) {
                winnerRequest.setKeyword(URLDecoder.decode(winnerRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<WinnerVo> winnerVoEasyUIGrid =iWinnerService.getWinnerInfoByPage(winnerRequest);
            model.addAttribute("winnerVos",winnerVoEasyUIGrid.getRows());
            model.addAttribute("currentPage",winnerRequest.getPage());
            model.addAttribute("totalPages",(winnerRequest.getTotalRecords() / winnerRequest.getRows()) + 1);
            model.addAttribute("keyword",winnerRequest.getKeyword());
        }catch (Exception e){
            logger.error("获取获奖用户列表页面" + e.getMessage());
        }
        return "/memberInfo/winnerInfo/index";
    }

    /**
     * 进入修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Long id,Model model){
        try {
            Prize prize = iWinnerService.getReceivingGoodById(id);
            model.addAttribute("prizeVos",prize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/memberInfo/winnerInfo/modify";
    }

    /**
     * 保存收货信息
     * @param prize
     * @return
     */
    @RequestMapping(value = "/modifyAction.json")
    @ResponseBody
    public JsonResult modifyAction(Prize prize) {
        try {
            if (iWinnerService.insertReceivingGoodAddress(prize)) {
                return new JsonResult();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }




    /**
     * author:赵杰
     * Business Description: 获取充值卡列表
     * @param model
     * @param winnerRequest
     * @return
     */
    @RequestMapping(value = "/rechargeCard.htm")
    public String rechargeCard(Model model, WinnerRequest winnerRequest){
        try {
            List<RechargeableCard> rechargeableCards=iRechargeableCardService.getListByPrizeId(winnerRequest.getPrizeId());
            model.addAttribute("rechargeableCardVos",rechargeableCards);
            model.addAttribute("prizeId",winnerRequest.getPrizeId());
        }catch (Exception e){
            logger.error("获取充值卡列表页面" + e.getMessage());
        }
        return "/memberInfo/winnerInfo/rechargeCardList";
    }


    /**
     * author:黄萧林
     * Business Description:进入充值卡修改界面
     * @param model
     * @param winnerRequest
     * @return
     */
    @RequestMapping(value = "/modifyRechargeCard.htm")
    public String modifyRechargeCard(Model model,WinnerRequest winnerRequest){
        try {
            RechargeableCard rechargeableCard = iRechargeableCardService.getRechargeableCard(winnerRequest.getId());
            model.addAttribute("rechargeableCardVo",rechargeableCard);
            model.addAttribute("prizeId",winnerRequest.getPrizeId());
        }catch (Exception e){
            logger.error("修改充值卡列表页面" + e.getMessage());
        }
        return "/memberInfo/winnerInfo/modifyRechargeCard";
    }

    /**
     * author:黄萧林
     * Business Description:修改充值卡信息
     * @param rechargeableCard
     * @return
     */
    @RequestMapping(value = "/modifyRechargeCard.json")
    @ResponseBody
    public JsonResult modifyRechargeCard(RechargeableCard rechargeableCard) {
        try {
            if (iRechargeableCardService.modifyRechargeCard(rechargeableCard)) {
                return new JsonResult();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

    /**
     * author:黄萧林
     * Business Description:删除充值卡信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteRechargeCard.json")
    @ResponseBody
    public JsonResult deleteRechargeCard(Long id){
        try {
            if (iRechargeableCardService.deleteRechargeCard(id)){
               return new JsonResult();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

}
